﻿package {
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	import flash.media.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.*;
	import fl.transitions.easing.*;
	import fl.transitions.Blinds;
	import fl.transitions.TransitionManager;
	import fl.transitions.Transition;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	
	public class Narrative extends MovieClip 
	{
		private var counter:Number;
		private var dialogueSound:Array;
		private var dialoguesArray:Array;
		private var syllabusArray:Array;
		public var movieFinished:Boolean;
		private var soundFinished:Boolean;
		private var soundChannel_1:SoundChannel;
		
		// this variable will be used to store the images
		private var imageLoader:Array;
		// this variable will be used to store the audio associated with the images
		private var soundFiles:Array;
		
		// this is the button to repeat the animation for a syllabus word
		private var repeat:MovieClip;
		
		// this is the button to move to the next word
		private var nextWord:MovieClip;
		
		// this stores the text to be displayed on the button
		private var buttonText1:TextField;
		private var buttonText2:TextField;
		private var textStyling:TextFormat;
		
		private var horizontalBar:ImageToMovie;
		
		// this variable will keep a track of the image being displayed
		private var imageTracker:int
		
		// used to store the return value of setInterval
		// setInterval will be used to put a delay of 1 sec
		// the delay is used for the gap between displaying image of a word
		// the L2 word spoken
		private var delay:uint;
		var myTransitionManager:TransitionManager;
		private var syllabusTween:Tween;
		private var imageNumber_1:int;
		
		private var motherBubbleTween:Tween;
		private var sonBubbleTween:Tween;
		
		private var motherBubbleTween2:Tween;
		private var sonBubbleTween2:Tween;
		
		private var channel_bubble:SoundChannel;
		
		public function Narrative(dialogues:Array, syllabus:Array)
		{
			dialogueSound = new Array(dialogues.length);
			syllabusArray = new Array(syllabus.length);
			channel_bubble = new SoundChannel();
			motherBubbleTween = new Tween(Market.mother_bubble,"alpha",Regular.easeIn,0,1,0.5,true);
			motherBubbleTween.stop();
			sonBubbleTween = new Tween(Market.son_bubble,"alpha",Regular.easeIn,0,1,0.5,true);
			sonBubbleTween.stop();
			motherBubbleTween2 = new Tween(Market.mother_bubble,"alpha",Regular.easeIn,1,0,0.5,true);
			motherBubbleTween2.stop();
			sonBubbleTween2 = new Tween(Market.son_bubble,"alpha",Regular.easeIn,1,0,0.5,true);
			sonBubbleTween2.stop();
			setupTheButtons();
			addEventListener(Event.ADDED_TO_STAGE,startKeyboardListener);
			dialoguesArray = dialogues;
			syllabusArray = syllabus;
			movieFinished = false;
			counter = 0;
			imageTracker =0;
			soundFinished = true;
			soundChannel_1 = new SoundChannel();
			loadDialogues();
			
			// initiliazing the size of this array to 5
			imageLoader = new Array();
			soundFiles = new Array();
			loadImages(Market.syllabus);
		}
		
		private function startKeyboardListener(e:Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyboardListener);
			removeEventListener(Event.ADDED_TO_STAGE,startKeyboardListener);
		}
		
		// This function displays a particular image from the array of the images
		// stored in imageLoader at index imageNumber
		// also displays name of the image in L1
		// and plays the sound in L2 after a delay of 1 sec
		private function showTheImage(imageNumber:int)
		{
			soundFinished = false;
			if (imageNumber != 0)
				imageLoader[imageNumber -1 ].visible = false;
			imageNumber_1 = imageNumber;
			imageLoader[imageNumber].showImage();
			syllabusTween = new Tween(imageLoader[imageNumber],"alpha",Regular.easeIn,0,1,0.5,true);
			syllabusTween.start();
			syllabusTween.addEventListener(TweenEvent.MOTION_FINISH,playTheSound);
			
			
		}
		
		
		// This function plays the sound stored in the variable soundFiles at
		// the index soundNumber
		private function playTheSound(e:Event):void
		{
			
			//if (soundFinished == true){
				soundFinished = false;
				soundChannel_1 = soundFiles[imageNumber_1].play();
				soundChannel_1.addEventListener(Event.SOUND_COMPLETE, soundIsComplete);
			//}
		}
		
		private function soundIsComplete(e:Event):void{
			soundFinished = true;
		}
		
		private function setupTheButtons():void
		{
			repeat = new MovieClip();
			nextWord = new MovieClip();
			horizontalBar = new ImageToMovie("Images/horizontalBar.jpg");
			
			addChild(horizontalBar);
			addChild(repeat);
			addChild(nextWord);
			
			repeat.visible = false;
			nextWord.visible = false;
			horizontalBar.hideImage();
		}
		private function initialiseTheButtons():void
		{
			
			
			horizontalBar.setX(0);
			horizontalBar.setY(453);
			horizontalBar.scaleX = 1.61;
			
			buttonText1 = new TextField();
			buttonText2 = new TextField();
			textStyling = new TextFormat();
			buttonText1.text = "Repeat";
			buttonText2.text = "Next";
			
			textStyling.size = 24;
			textStyling.color = 0xFFFFFF;
			buttonText1.setTextFormat(textStyling);
			buttonText2.setTextFormat(textStyling);
			
			
			horizontalBar.showImage();
			repeat.visible = true;
			nextWord.visible = true;
			repeat.addChild(buttonText1);
			
			repeat.x = 20;
			repeat.y = 450;
			nextWord.addChild(buttonText2);
			
			nextWord.x = 700;
			nextWord.y = 450;
			//nextWord.addEventListener(MouseEvent.CLICK, showNextImage);
			//repeat.addEventListener(MouseEvent.CLICK, repeatTheImage);
		}
		
		private function myKeyboardListener(e:KeyboardEvent):void
		{
			if (repeat.visible == true)
			{
				if (e.keyCode == Keyboard.SPACE)
				{
					if (soundFinished == true)
					repeatTheImage();
				}
				
				else if (e.keyCode == Keyboard.ENTER)
				{
					if (soundFinished == true)
						showNextImage();
				}
			}
		}
		
		private function showNextImage():void
		{
			imageTracker = imageTracker + 1;
			
			if (imageTracker == 5)
			{
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyboardListener);
				movieFinished = true;
				repeat.visible = false;
				nextWord.visible = false;
				removeChild(horizontalBar)
				imageLoader[4].visible = false;
				//nextWord.removeEventListener(MouseEvent.CLICK, showNextImage);
				//repeat.removeEventListener(MouseEvent.CLICK, repeatTheImage);
				
				return;
			}
			showTheImage(imageTracker);					
		}
		
		private function repeatTheImage():void {
			if (soundFinished == true)
				showTheImage(imageTracker);
		}
		
		
		// this function loads the images which need to be taught 
		// to the child in the variable imageLoader
		// this function also loads the sounds corresponding to those images in the variable soundFiles
		// parameters 
		// 1. list : this is the array which stores the words which need to be taught
		private function loadImages(list:Array)
		{
			// this variable is used to traverse the variable "list"
			var counter_1:Number = 0;
			while(counter_1<list.length)
			{
				// store the images in the imageLoader array						
				
				imageLoader.push(new ImageToMovie("Images/" + list[counter_1].image));
				soundFiles.push(new Sound());
				soundFiles[counter_1].load(new URLRequest("Audio/" + list[counter_1].audio));
				
				this.addChild(imageLoader[counter_1]);
				
				// hide the images
				//imageLoader[counter_1].visible = false;
				imageLoader[counter_1].hideImage();
				// bring the images to the bottom left corner of the screen.
				imageLoader[counter_1].setX(40);
				imageLoader[counter_1].setY(140);
				
				// shrink the size of the images by half				
				imageLoader[counter_1].scaleX = 1;
				imageLoader[counter_1].scaleY= 1;				
				
				counter_1 = counter_1 + 1;
			}
		}
		
		
		//Load the audio files for dialogues
		private function loadDialogues():void
		{
			var i:int=0;
			for(i=0;i<dialoguesArray.length;i++)
			{
				dialogueSound[i] = new Sound(new URLRequest("Audio/"+dialoguesArray[i].audio));
			}
			dialogueSound[i-1].addEventListener(Event.COMPLETE, playDialogues);
		}
		private function motherDialogueStarted(e:Event):void
		{
			channel_bubble = dialogueSound[counter].play();
			counter++;
			channel_bubble.addEventListener(Event.SOUND_COMPLETE, playDialogues);
		}
		private function sonDialogueFinished(e:Event):void
		{
			Market.mother_bubble.mother_speech.text=dialoguesArray[counter].text;
			Market.son_bubble.son_speech.text="";
			Market.mother_bubble.visible=true;
			motherBubbleTween.start();
			motherBubbleTween.addEventListener(TweenEvent.MOTION_FINISH,motherDialogueStarted);
			Market.son_bubble.visible=false;
		}
		private function motherDialogueFinished(e:Event):void
		{
			Market.son_bubble.visible = true;
			Market.son_bubble.son_speech.text=dialoguesArray[counter].text;
			Market.mother_bubble.mother_speech.text="";
			Market.mother_bubble.visible=false;
			sonBubbleTween.start();
			sonBubbleTween.addEventListener(TweenEvent.MOTION_FINISH,motherDialogueStarted);				
		}
		private function playDialogues(soundEvent:Event):void
		{
			//var channel:SoundChannel = new SoundChannel();
			if(counter<dialoguesArray.length)
			{
				if(dialoguesArray[counter].speaker=="Mother")
				{
					sonBubbleTween2.start();
					sonBubbleTween2.addEventListener(TweenEvent.MOTION_FINISH,sonDialogueFinished);
														
				}
			
				else if(dialoguesArray[counter].speaker=="Raju")
				{
					motherBubbleTween2.start();
					motherBubbleTween2.addEventListener(TweenEvent.MOTION_FINISH,motherDialogueFinished);
										
				}
			}
			else
			{
				Market.mother_bubble.visible=false;
				Market.son_bubble.visible=false;
				initialiseTheButtons();
				showTheImage(imageTracker);
				return;
			}
		}
	}
}